package Stock.StockGUIS;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;
import com.toedter.calendar.*;
import java.text.SimpleDateFormat;
import java.sql.*;
import Stock.StockDAOS.*;

public class StockAddNewPanel extends JPanel implements ActionListener
{
	private JPanel southP=null;
	JButton addB=null, resetB=null;
	StockDetailsPanel sdp=null;
	StockNewManager snm=null;
	java.util.Date date=null;
	SimpleDateFormat sdf=null;
	String bdate=null;
	Connection Conn;
	String queryCatId =	"SELECT catId FROM priceCatTable WHERE catName = ?";
	String queryStockType =	"SELECT stockTypeId FROM stockTypeTable WHERE stockTypeName = ?";
	
	public StockAddNewPanel(StockDetailsPanel sdp, Connection Conn) 
	{
		this.sdp = sdp;
		this.Conn = Conn;
		snm = new StockNewManager(Conn);
		sdf = new SimpleDateFormat("yyyy-MM-dd");
		southP = new JPanel(); 
		BoxLayout b4 = new BoxLayout(southP, BoxLayout.X_AXIS);
		southP.setLayout(b4);
		addB = new JButton(" Add New Stock Item ");
		
		addB.addActionListener(this);
		resetB = new JButton(" Reset Field's ");
		resetB.addActionListener(this);
		
		southP.add(resetB);
		southP.add(addB);	
		southP.setBorder(BorderFactory.createEmptyBorder(5,10,5,10));
		
		this.add(southP);
	}	
	
	public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
		if(source == addB)
		{
			if((sdp.stockTypeJcb.getSelectedItem().equals(""))||(sdp.titleTxt.equals(""))||
				(sdp.statusJcb.getSelectedItem().equals(""))||
				(sdp.catIdJcb.getSelectedItem().equals("")))
			{
				JOptionPane.showMessageDialog(new JFrame(), "You are required to enter values for the following fields :\nMedia Type Id, Title, Status and Category Id!");
				sdp.stockTypeJcb.requestFocus();
			}
			else
			{
				if(sdp.lastJdc.getDate()==null)
				{
					bdate = "1900-01-01";
				}
				else
				{
					java.util.Date date = sdp.lastJdc.getDate();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					bdate = sdf.format(date);
				}
				String temp1 = (String)sdp.catIdJcb.getSelectedItem();
				String temp2 = convertCatName(temp1);
				
				String temp4 = (String)sdp.stockTypeJcb.getSelectedItem();
				String temp5 = convertStockType(temp4);
 								
				String rdate = sdp.releaseDateTxt.getText();
				
				String [] attr = new String [17];
				attr[0] = snm.generateStockId();
				attr[1] = temp5;
				System.out.println("media type Id = "+ attr[1] );
				attr[2] = sdp.titleTxt.getText();
				attr[3] = (String)sdp.statusJcb.getSelectedItem();
				attr[4] = sdp.barcodeTxt.getText();
				attr[5] = sdp.brandTxt.getText();
				attr[6] = sdp.serialNoTxt.getText();
				attr[7] = sdp.descriptionTxt.getText();
				attr[8] = temp2;
				attr[9] = sdp.runTimeTxt.getText();
				int temp = rdate.length();
				if(temp > 4)
				{
					JOptionPane.showMessageDialog(new JFrame(), "Release date requires a year entry only!\nE.g. (1999)");
					sdp.releaseDateTxt.requestFocus();
					return;
				}
				else
				{
					attr[10] = rdate;
				}
				attr[11] = (String)sdp.genreJcb.getSelectedItem();
				attr[12] = sdp.ageRestTxt.getText();
				attr[13] = sdp.directorTxt.getText();
				attr[14] = sdp.actor1Txt.getText();
				attr[15] = sdp.actor2Txt.getText();
				attr[16] = bdate;
							
				for(int i = 0; i < attr.length; i++)
				{
					System.out.println("all attributes "+attr[i]);
				}
						
				StockNew stock = new StockNew(attr);
				snm.commitStock(stock);	
				JOptionPane.showMessageDialog(this, "New Stock Item Saved!");
				sdp.mediaStockIdTxt.setText("");
				sdp.stockTypeJcb.setSelectedItem("");
				sdp.barcodeTxt.setText("");
				sdp.brandTxt.setText("");
				sdp.serialNoTxt.setText("");
				sdp.descriptionTxt.setText("");
				sdp.titleTxt.setText("");
				sdp.catIdJcb.setSelectedItem("");
				sdp.runTimeTxt.setText("");
				sdp.ageRestTxt.setText("");
				sdp.directorTxt.setText("");
				sdp.actor1Txt.setText("");
				sdp.actor2Txt.setText("");
				sdp.releaseDateTxt.setText("");
				sdp.genreJcb.setSelectedItem("");
				sdp.statusJcb.setSelectedItem("");
				sdp.lastJdc.setDate(null);
			}	
		}
		if(source == resetB)
		{
			sdp.mediaStockIdTxt.setText("");
			sdp.stockTypeJcb.setSelectedItem("");
			sdp.barcodeTxt.setText("");
			sdp.brandTxt.setText("");
			sdp.serialNoTxt.setText("");
			sdp.descriptionTxt.setText("");
			sdp.titleTxt.setText("");
			sdp.catIdJcb.setSelectedItem("");
			sdp.runTimeTxt.setText("");
			sdp.ageRestTxt.setText("");
			sdp.directorTxt.setText("");
			sdp.actor1Txt.setText("");
			sdp.actor2Txt.setText("");
			sdp.releaseDateTxt.setText("");
			sdp.genreJcb.setSelectedItem("");
			sdp.statusJcb.setSelectedItem("");
			sdp.lastJdc.setDate(null);
		}
	}
	//ConvertCatName takes the category name from the Combobox and
	//gets the CategoryId from the DB to be entered into the Stock table
	//allowing the user to not have to know the different catId's. 
	public String convertCatName(String inputString)
	{
		String input = inputString;
		try
		{
			PreparedStatement pstmt = Conn.prepareStatement(queryCatId);
			ResultSet rs;
			pstmt.setString(1, input);
			rs = pstmt.executeQuery();
			rs.next();
			String temp3 = rs.getString(1);
			return temp3;	
		}
		catch(SQLException sqle)
		{
			sqle.printStackTrace();
			return "";
		}
		 
	}
	//ConvertStockType takes the stock type name from the Combo box and get
	//the stock type Id from the DB which means the user does not need to know
	//the different stockTypeId's.
	public String convertStockType(String inputString)
	{
		String input = inputString;
		try
		{
			PreparedStatement pstmt = Conn.prepareStatement(queryStockType);
			ResultSet rs;
			pstmt.setString(1, input);
			rs = pstmt.executeQuery();
			rs.next();
			String temp3 = rs.getString(1);
			return temp3;	
		}
		catch(SQLException sqle)
		{
			sqle.printStackTrace();
			return "";
		}
		 
	}
}
